﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Controls;

namespace HobSoft.SoccerBet2014.Common.Wpf.Behavior
{
    public class ExecuteCommandAction : TargetedTriggerAction<Control>
    {
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter",
                typeof(object), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command",
                typeof(ICommand), typeof(ExecuteCommandAction), new FrameworkPropertyMetadata(null));

        public ICommand Command
        {
            get
            {
                return (ICommand)this.GetValue(CommandProperty);
            }
            set
            {
                this.SetValue(CommandProperty, value);
            }
        }
        public object CommandParameter
        {
            get
            {
                return this.GetValue(CommandParameterProperty);
            }
            set
            {
                this.SetValue(CommandParameterProperty, value);
            }
        }

        protected override void Invoke(object parameter)
        {
            if (this.Command != null)
            {
                if (this.Command.CanExecute(this.CommandParameter))
                {
                    this.Command.Execute(this.CommandParameter);
                }
            }
        }
    }
}
